<template>
  <div class="lottie-container" :style="{ width, height }">
    <div ref="lottieElement"></div>
  </div>
</template>

<script>
import { onMounted, onUnmounted } from 'vue'
import lottie from 'lottie-web'

export default {
  name: 'LottiePlayer',
  props: {
    animationPath: {
      type: String,
      required: true
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100%'
    },
    loop: {
      type: Boolean,
      default: true
    },
    autoplay: {
      type: Boolean,
      default: true
    }
  },
  setup(props) {
    let animation = null

    onMounted(() => {
      animation = lottie.loadAnimation({
        container: document.querySelector('.lottie-container div'),
        renderer: 'svg',
        loop: props.loop,
        autoplay: props.autoplay,
        path: props.animationPath
      })
    })

    onUnmounted(() => {
      if (animation) {
        animation.destroy()
      }
    })
  }
}
</script>

<style scoped>
.lottie-container {
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
